package com.dstz.crm.core.manager.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.api.exception.BusinessMessage;
import com.dstz.base.api.query.QueryFilter;
import com.dstz.base.api.query.QueryOP;
import com.dstz.base.core.id.IdUtil;
import com.dstz.base.core.util.StringUtil;
import com.dstz.base.manager.impl.BaseManager;
import com.dstz.bpm.api.engine.action.cmd.ActionCmd;
import com.dstz.crm.core.dao.CustomerContractDao;
import com.dstz.crm.core.dao.CustomerDao;
import com.dstz.crm.core.manager.CustomerContractManager;
import com.dstz.crm.core.manager.CustomerManager;
import com.dstz.crm.core.manager.CustomerRelationManager;
import com.dstz.crm.core.model.Customer;
import com.dstz.crm.core.model.CustomerContract;
import com.dstz.crm.core.model.CustomerRelation;
import com.dstz.crm.core.model.Product;
import com.dstz.org.api.model.IUser;
import com.dstz.org.api.service.UserService;
import com.dstz.sys.api.model.IDataDict;
import com.dstz.sys.api.service.DataDictService;
import com.dstz.sys.util.ContextUtil;

import cn.hutool.core.collection.CollectionUtil;
/**
 * 案例 Manager处理实现类
 * @author aschs
 * @email aschs@qq.com
 * @time 2018-08-24 18:06:04
 */
@Service("customerContractManager")
public class CustomerContractManagerImpl extends BaseManager<String, CustomerContract> implements CustomerContractManager{
	@Resource
	CustomerContractDao customerContractDao;
	@Resource
	CustomerRelationManager customerRelationMananger;
	@Resource
	CustomerManager customerManager;
	 
	@Override
	public boolean isCustomerContractExist(CustomerContract customerContract) {
		return customerContractDao.isCustomerContractExist(customerContract)>0;
	}
	
	
	@Override
	public CustomerContract get(String entityId) {
		CustomerContract customerContract =  super.get(entityId);
		if(customerContract!= null) {
			customerContract.setCustomerRelationList(customerRelationMananger.getContractRelation(entityId,"contractProduct"));
			String customerId = customerContract.getCustomerId();
			if(StringUtil.isNotEmpty(customerId)) {
				List<Customer> customerList=new ArrayList<>();
				Customer customer = customerManager.get(customerId);
				if(customer!=null)
					customerList.add(customer);
				customerContract.setCustomerList(customerList);
			}
			
		}
		return customerContract;
	}

	@Override
	public void saveCustomerContract(CustomerContract customerContract) {
		if(customerContract.getCustomerList()==null||customerContract.getCustomerList().size()==0) {
			throw new BusinessMessage("成交客户不能为空!");
		}
		customerContract.setCustomerId(customerContract.getCustomerList().get(0).getId());
		if(StringUtil.isEmpty(customerContract.getId())) {
			this.create(customerContract);
		}else {
			this.update(customerContract);
			customerRelationMananger.removeByContractId(customerContract.getId());
		}
		
		List<CustomerRelation> customerRelationList = customerContract.getCustomerRelationList();
		
		if(CollectionUtil.isEmpty(customerRelationList)) return;
		
		customerRelationList.forEach( rel ->{
			rel.setContractId(customerContract.getId());
			customerRelationMananger.create(rel);
		});
		 
	}


	@Override
	public void saveCustomerContractJson(ActionCmd actionCmd) {
		String actionName = actionCmd.getActionName();
		String bizKey = actionCmd.getBusinessKey();
		JSONObject object = actionCmd.getBusData();
		Map<String, Object> actionVariables = actionCmd.getActionVariables();
		CustomerContract customerContract = JSON.toJavaObject(object, CustomerContract.class);
		if (isCustomerContractExist(customerContract)) throw new BusinessMessage("合同编号在系统中已存在!");
		switch (actionName) {
		case "start":
			customerContract.setAudit("0");
			customerContract.setAuditTime(null);
			customerContract.setAuditUserId(null);
			if(customerContract.getCustomerList()==null||customerContract.getCustomerList().size()==0) {
				throw new BusinessMessage("成交客户不能为空!");
			}
			customerContract.setCustomerId(customerContract.getCustomerList().get(0).getId());
			break;
		case "draft":
			customerContract.setAudit("0");
			customerContract.setAuditTime(null);
			customerContract.setAuditUserId(null);
			break;
		case "agree":
			customerContract.setAudit("1");
			customerContract.setAuditTime(new Date());
			customerContract.setAuditUserId(ContextUtil.getCurrentUserId());
			break;
		case "oppose":
			customerContract.setAudit("2");
			customerContract.setAuditTime(new Date());
			customerContract.setAuditUserId(ContextUtil.getCurrentUserId());
			break;
		case "agree1":
			customerContract.setAudit("0");
			customerContract.setAuditTime(null);
			customerContract.setAuditUserId(null);
			break;
		case "oppose1":
			customerContract.setAudit("0");
			customerContract.setAuditTime(null);
			customerContract.setAuditUserId(null);
			break;
		default:
			break;
		}
		if(StringUtil.isEmpty(bizKey)) {
			String id = IdUtil.getSuid();
			actionCmd.setBusinessKey(id);
			customerContract.setId(id);
			customerContractDao.create(customerContract);
			
//			Map<String,Object> hashMap = new HashMap<>();
//			hashMap.put("startVariable", customerContract.getAudit());
			//启动时候设置一些流程变量，请看 act_ru_variable 表、在整个流程声明周期您都可以使用该流程变量，可以用于分支判断等等
//			actionCmd.setActionVariables(hashMap);
		}else {
			customerContractDao.update(customerContract);
			customerRelationMananger.removeByContractId(customerContract.getId());
//			Map<String,Object> hashMap = new HashMap<>();
//			hashMap.put("doTaskVariable", customerContract.getAudit());
//			actionCmd.setActionVariables(hashMap);
		}
//		String jsonString = JSONObject.toJSONString(customerContract);
//		JSONObject parseObject = JSON.parseObject(jsonString);
//		Map<String,Object> hashMap = new HashMap<>();
//		hashMap.put("doTaskVariable", parseObject);
//		actionCmd.setActionVariables(hashMap);
		
		List<CustomerRelation> customerRelationList = customerContract.getCustomerRelationList();
		
		if(CollectionUtil.isEmpty(customerRelationList)) return;
		
		customerRelationList.forEach( rel ->{
			rel.setContractId(customerContract.getId());
			customerRelationMananger.create(rel);
		});
	}


//	@Override
//	public Demo get(String entityId) {
//		Demo demo = super.get(entityId);
//		if(demo == null) return null;
//		
//		List<DemoSub> demoSublist = demoSubMananger.getByFk(entityId);
//		demo.setDemoSubList(demoSublist);
//		
//		return demo;
//	}
	
	
	

	/**
	 * URL 表单案例
	 */
//	@Override
//	public void saveDemoJson(ActionCmd actionCmd) {
//		String bizKey = actionCmd.getBusinessKey();
//		JSONObject object = actionCmd.getBusData();
//		Product demo = JSON.toJavaObject(object, Product.class);
//		
//		if(StringUtil.isEmpty(bizKey)) {
//			String id = IdUtil.getSuid();
//			actionCmd.setBusinessKey(id);
//			demo.setId(id);
//			productDao.create(demo);
//			
//			Map<String,Object> hashMap = new HashMap<>();
//			hashMap.put("startVariable", demo.getName());
//			//启动时候设置一些流程变量，请看 act_ru_variable 表、在整个流程声明周期您都可以使用该流程变量，可以用于分支判断等等
//			actionCmd.setActionVariables(hashMap);
//		}else {
//			productDao.update(demo);
//			Map<String,Object> hashMap = new HashMap<>();
//			hashMap.put("doTaskVariable", demo.getName() + "-" + actionCmd.getActionName());
//			actionCmd.setActionVariables(hashMap);
//		}
//	}
	
}
